﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using EntityFrameworkETL.HelloWorld.Models;
using EntityFrameworkETL.SaveBatchedChanges;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace EntityFrameworkETL.Test
{
    [TestClass]
    public class CommandParserTest
    {
        [TestMethod]
        public void ParseFkParametersTest()
        {
            var sqlPerson = @"INSERT [dbo].[People]([Name], [WorldId])
VALUES (@0, @1)
SELECT [Id]
FROM [dbo].[People]
WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()";
            DbCommand dbCommand  = new SqlCommand(sqlPerson);
            dbCommand.Parameters.Add(new SqlParameter("@0", "Chris"));
            dbCommand.Parameters.Add(new SqlParameter("@1", 2));

            var commandParser = new CommandParser();
            var dictionary = commandParser.ParseFkParameters(dbCommand);
            Assert.IsFalse(dictionary["@0"]);
            Assert.IsTrue(dictionary["@1"]);
        }

        [TestMethod]
        public void Test()
        {
            List<DbCommand> commands = new List<DbCommand>();
            commands.Add(TestExtensions.CreateWorldCommand("Earth")); //-1
            commands.Add(TestExtensions.CreatePersonCommand("Megan", -1));  //-2
            commands.Add(TestExtensions.CreateJobComamand("Chemist", -2));//-3
            commands.Add(TestExtensions.CreateJobComamand("GameSalad", -2));
            commands.Add(TestExtensions.CreatePersonCommand("Chris", -1));

            var fakeReaderGenerator = new FakeReaderGenerator();
            var commandParser = new CommandParser();
            var commandDbContextSync = new CommandDbContextSync();
            var commandCacheStructurer = new CommandCacheStructurer();
            var batchCommands = new BatchCommands();
            var executeBatchedCommand = new ExecuteBatchedCommand();
            var fixupCommands = new FixupCommands();
            //using (var dbContext = new UsersContext())
            //{
                int fakeId = 0;
                List<CommandCache> commandCaches = new List<CommandCache>();
                foreach (var dbCommand in commands)
                {
                    
                    fakeId -= 1;
                    var commandCache = new CommandCache(dbCommand);
                    commandCache.FakeId = fakeId;
                    commandCaches.Add(commandCache);
                }

                commandCaches.ToList().ForEach(x =>
                {
                    x.TableName = commandParser.ParseTableName(x.DbCommand);
                    x.ParameterIsFkDictionary = commandParser.ParseFkParameters(x.DbCommand);
                });
                //commandDbContextSync.Sync(dbContext, commandCaches);
                var structuredCommandCaches = commandCacheStructurer.GeStructuredCommandCaches(commandCaches);
                Assert.IsTrue(structuredCommandCaches.First().ChildCommands.Count == 2);
                //var batchedCommands = batchCommands.BatchAndExecute(dbContext, structuredCommandCaches, executeBatchedCommand, fixupCommands);


           // }
            
            

            //return structuredCommandCaches.Count();
        }
    }


}
